import 'package:flutter/material.dart';
import 'package:flutter_base/common/utils/utils.dart';
import 'package:flutter_base/common/values/values.dart';
import 'package:flutter_base/common/widgets/widgets.dart';
import 'package:flutter_base/pages/addressbook/addressbook.dart';
import 'package:flutter_base/pages/cooperation/cooperation.dart';
import 'package:flutter_base/pages/home/home.dart';
import 'package:flutter_base/pages/me/me.dart';

class ApplicationPage extends StatefulWidget {
  ApplicationPage({ Key? key}) : super(key: key);

  @override
  _ApplicationPageState createState() => _ApplicationPageState();
}

class _ApplicationPageState extends State<ApplicationPage>
    with SingleTickerProviderStateMixin {
  // 当前 tab 页码
  int _page = 0;

  // tab 页标题
  // final List<String> _tabTitles = ['Creatoo', 'Cagegory', 'Bookmarks', 'Me'];

  // 页控制器
  late PageController _pageController;

  // 底部导航项目
  final List<BottomNavigationBarItem> _bottomTabs = <BottomNavigationBarItem>[
    new BottomNavigationBarItem(
      icon: Icon(
        Iconfont.home,
        color: AppColors.tabBarElement,
      ),
      activeIcon: Icon(
        Iconfont.home,
        color: AppColors.primaryElement,
      ),
      title: Text('首页'),
      backgroundColor: AppColors.primaryElement,
    ),
    new BottomNavigationBarItem(
      icon: Icon(
        Iconfont.cooperation,
        color: AppColors.tabBarElement,
      ),
      activeIcon: Icon(
        Iconfont.cooperation,
        color: AppColors.primaryElement,
      ),
      title: Text('协作'),
      backgroundColor: AppColors.primaryElement,
    ),
    new BottomNavigationBarItem(
      icon: Icon(
        Iconfont.addressbook,
        color: AppColors.tabBarElement,
      ),
      activeIcon: Icon(
        Iconfont.addressbook,
        color: AppColors.primaryElement,
      ),
      title: Text('通讯录'),
      backgroundColor: AppColors.primaryElement,
    ),
    new BottomNavigationBarItem(
      icon: Icon(
        Iconfont.me,
        color: AppColors.tabBarElement,
      ),
      activeIcon: Icon(
        Iconfont.me,
        color: AppColors.primaryElement,
      ),
      title: Text('我的'),
      backgroundColor: AppColors.primaryElement,
    ),
  ];

  // tab栏动画
  void _handleNavBarTap(int index) {
    _pageController.animateToPage(index,
        duration: const Duration(milliseconds: 200), curve: Curves.ease);
  }

  // tab栏页码切换
  void _handlePageChanged(int page) {
    setState(() {
      this._page = page;
    });
  }

  @override
  void initState() {
    super.initState();
    _pageController = new PageController(initialPage: this._page);
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  // 顶部导航
  Widget _buildAppBar() {
    return transparentAppBar(
      context: context,
      title: Text(
        'tabTitles[_page]',
        style: TextStyle(
          color: AppColors.primaryText,
          fontFamily: 'Montserrat',
          fontSize: zhiSetFontSize(18.0),
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

  // 内容页
  Widget _buildPageView() {
    return PageView(
      physics: NeverScrollableScrollPhysics(),
      children: <Widget>[
        HomePage(),
        CooperationPage(),
        AddressBookPage(),
        MePage(),
      ],
      controller: _pageController,
      onPageChanged: _handlePageChanged,
    );
  }

  // 底部导航
  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      items: _bottomTabs,
      currentIndex: _page,
      fixedColor: AppColors.primaryElement,
      type: BottomNavigationBarType.fixed,
      onTap: _handleNavBarTap,
      showSelectedLabels: true,
      showUnselectedLabels: true,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: _buildAppBar(),
      body: _buildPageView(),
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }
}
